<!--  多折线 -->
<template>
  <el-card>
    <template #header> 实时数据 </template>
    <div :id="id" :class="className" :style="{ height, width }" />
  </el-card>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';

const props = defineProps({
  id: {
    type: String,
    default: 'lineChart'
  },
  className: {
    type: String,
    default: ''
  },
  width: {
    type: String,
    default: '200px',
    required: true
  },
  height: {
    type: String,
    default: '200px',
    required: true
  },
  info: {
    type: Array,
    default: []
  }
});
const xa:any = []
const se:any = []
const info:any = props.info

info.forEach((item:any) => {
  xa.push(item.time)
  se.push(item.data)
})
const options = {
  title: {
    text: info[0].dataType
  },
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    data: xa.sort()
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: se.sort(),
      type: 'line',
      smooth: true
    }
  ]
};

onMounted(() => {
  // options.title.text = props.info[0].deviceName
  // 图表初始化
  const chart = echarts.init(
    document.getElementById(props.id) as HTMLDivElement
  );
  chart.setOption(options);

  // 大小自适应
  window.addEventListener('resize', () => {
    chart.resize();
  });
});
</script>
